package com.dwd.check.controller;

import com.dwd.basic.utils.AjaxResult;
import com.dwd.basic.utils.PageBean;
import com.dwd.basic.utils.SendMail;
import com.dwd.check.pojo.Check;
import com.dwd.check.query.ShopQuery;
import com.dwd.check.service.ICheckService;
import com.dwd.org.mapper.ShopMapper;
import com.dwd.org.pojo.DictionaryType;
import com.dwd.org.pojo.Employee;
import com.dwd.org.pojo.Shop;
import com.dwd.org.query.DictionaryTypeQuery;
import com.dwd.org.service.IEmployeeService;
import com.dwd.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/16 21:13
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "/check")
public class CheckController {

    @Autowired
    private IShopService shopService;

    @Autowired
    private ICheckService checkService;

    @Autowired
    private SendMail sendMail;

    /**
     * 列表展示
     *
     * @param query
     * @return
     */
    @PostMapping
    public PageBean<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }

    /**
     * 通过，并且发送邮件
     *
     * @param id
     * @return
     */
    @PostMapping("/{id}")
    public AjaxResult pass(@PathVariable("id") Long id) {
        try {
            shopService.updateById(id);
            String email = shopService.queryEmailById(id);
//            发送邮件
            sendMail.sendMail(true, "激活信息", "<a href='http://localhost/check/active/" + id + "'>点此去激活</a>", true, "1097466381@qq.com", email);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    /**
     * 驳回把原因存储
     *
     * @param check
     * @return
     */
    @PostMapping("/turn")
    public AjaxResult turn(@RequestBody Check check) {
        try {
            //存储驳回原因
            checkService.save(check);
            //驳回修改状态为-1
            shopService.updateTurnById(check.getShop_id());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    /**
     * 激活
     *
     * @param id
     * @return
     */
    @GetMapping("/active/{id}")
    public AjaxResult active(@PathVariable("id") Long id) {
        try {
            //修改状态为2
            shopService.updatePassById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    /**
     * 查询驳回信息
     *
     * @param id
     * @return
     */
    @PostMapping("/queryReturn/{id}")
    public AjaxResult queryReturn(@PathVariable("id") Long id) {
        try {
            List<Check> list = checkService.queryReturn(id);
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    /**
     * 批量修改通过 状态为1 待激活
     * @param shops
     * @return
     */
    @PostMapping("/batchUpdate")
    public AjaxResult batchUpdate(@RequestBody List<Shop> shops) {
        try {
            //批量修改
            shopService.batchUpdate(shops);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

}
